from Accounting.CashFlow.SubRed import *
from Accounting.Entity.Feeder import *
from Accounting.Entity.Client import *
from Accounting.Entity.Tree import EntityTree
from Accounting.Entity.Utils import *
from Error.ExtendError import EntityNA

from DataAccess.DBConnFactory import DBConnFactory
from string import Template


#perfee_adj_conf format:
#(parent_feetp4_perfadj, )

#rebate_conf format:
#(parent_feetp4_rebate,rebate_ratio, )
def open_day_create_client(client_id, perfee_adj_conf=None, rebate_conf=None):
	conn = DBConnFactory().get_db_connection('ACCOUNTING')
	
	sql_text = '''insert into fee_conf
				(entity,class,subtype,sequence,parent_feetp4_perfadj,
				parent_feetp4_rebate,rebate_ratio,is_redem,schedule,curncy)
				values(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10)'''
				
	if perfee_adj_conf:
		v = (client_id,'ClientFloatFeeAdj','perf_fee_adj',2,'inv_perf_fee',None,None,'T','ME_Float','USD')
		conn.cursor().execute(sql_text, v)
		
	if rebate_conf:
		v = (client_id,'ClientFixedFeeRebate','mgt_fee_rebate',1,None,'inv_mgt_fee',0.5,'T','ME_Fixed','USD')
		conn.cursor().execute(sql_text, v)
		
	conn.commit()
	

def open_day_purchase(ref_date, client_id, cash_amt, currency, 
					feeder_id=None, master_id=None):
	client = EntityTree().find_node(client_id)
	if not client:		
		parent_feeder = EntityTree().find_node(feeder_id)
		if not parent_feeder:
			parent_master = EntityTree().find_node(master_id)
			assert parent_master			
			#update to runtime object 
			parent_feeder = Feeder(feeder_id, parent_master)
			parent_master.add_child(feeder_id, parent_feeder)
			
		client = Client(client_id, parent_feeder)		
		parent_feeder.add_child(client_id, client)
		
	conv_by = share_amt_conv_type(ref_date, client)
	#print 'Active Subscription @',ref_date.isoformat(), 'conv_by=', conv_by
	action = Subscription(ref_date, cash_amt, currency, conv_by, client, 'active')
	client.purchase_fund(action)
	

def open_day_redemption(ref_date, client_id, 
					cash_amt, currency, share_amt, ByCash, 
					corresponding_subscription_id):
					
	client = EntityTree().find_node(client_id)
	if client:
		conv_by = share_amt_conv_type(ref_date, client)
		
		if ByCash:
			type = RedemptionByAmount
			amount = cash_amt
		else:
			type = RedemptionByShare
			amount = share_amt
		#print 'Active Redemption @',ref_date.isoformat(), 'conv_by=', conv_by	
		action = Redemption(ref_date, amount, currency, type, 
						corresponding_subscription_id, conv_by, client, 'active')
		client.redeming_fund(action)
	else:
		raise EntityNA(), ('Client entity not exist, id='+client_id)
		


def open_day_redemption_by_feeder(ref_date, feeder_id, cash_amt, currency):
	feeder = EntityTree().find_node(feeder_id)
	if feeder:
		assert feeder.parent
		master = feeder.parent
		action = Redemption(ref_date, cash_amt, currency, RedemptionByAmount, 
						None, 'NAV', feeder, 'active')
		master.redeming_fund(action)
	else:
		raise EntityNA(), ('Feeder entity not exist, id='+feeder_id)
	
	
def open_day_purchase_by_feeder(ref_date, feeder_id, cash_amt, currency):
	feeder = EntityTree().find_node(feeder_id)
	if feeder:
		assert feeder.parent
		master = feeder.parent
		action = Subscription(ref_date, cash_amt, currency, 'NAV', feeder, 'active')
		master.purchase_fund(action)
	else:
		raise EntityNA(), ('Feeder entity not exist, id='+feeder_id)
		
		